package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.ui.home.activity.BuyLessonActivity;
import com.hongyuan.sellfitness.ui.home.beans.StoreLessonDetailBean;
import com.hongyuan.sellfitness.ui.main.activity.WebActivity;

import java.util.Arrays;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class StoreLessonDetailsViewModel extends CustomViewModel<MyRepository> {

    private  StoreLessonDetailBean bean;

    public ObservableField<StoreLessonDetailBean> entity = new ObservableField<>();

    public StoreLessonDetailsViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<StoreLessonDetailPriceItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<StoreLessonDetailPriceItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_store_lesson_detail_price);

    //给RecyclerView添加ObservableList
    public ObservableList<StringsImageViewModel> imgList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<StringsImageViewModel> imgBinding = ItemBinding.of(BR.viewModel, R.layout.item_images);

    //上课须知
    public BindingCommand goWeb = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","http://www.hongyuangood.com/courseNotice/index.html");
            bundle.putString("title","私教课上课须知");
            startActivity(WebActivity.class,bundle);
        }
    });

    //拨打电话
    public BindingCommand goCallTel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getUC().getCallTel().setValue(bean.getM_mobile());
        }
    });

    //拨打电话
    public BindingCommand goNext = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("cp_id",String.valueOf(entity.get().getCp_id()));
            startActivity(BuyLessonActivity.class,bundle);
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_COURSE_PRIVITE_INFO){
            bean = (StoreLessonDetailBean)dataBean;
            entity.set(bean);

            for(StoreLessonDetailBean.PriceListBean priceListBean : bean.getPrice_list()){
                StoreLessonDetailPriceItemViewModel itemViewModel = new StoreLessonDetailPriceItemViewModel(this,priceListBean);
                observableList.add(itemViewModel);
            }

            if(BaseUtil.isValue(bean.getCp_anli_imgs())){
                List<String> imgs = Arrays.asList(bean.getCp_anli_imgs().split(","));
                for(String img:imgs){
                    StringsImageViewModel itemViewModel = new StringsImageViewModel(this,img);
                    imgList.add(itemViewModel);
                }
            }

        }
    }
}
